Here's a useful snippet for sending HTML formatted messages with the JavaMail API. One problem that I see out there is that people like nicely formatted HTML messages, but they don't often include a text-only part as well. This makes the message inaccessible to certain mail clients. This Mailer class shows an example of what I consider to be best practice in this arena.
Enjoy!
// $Header$
// $Name$
package com.sherwin.ordering.util;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Mailer {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
if (args.length <>
throw new IllegalArgumentException("2 arguments required");
}
String email1 = args[0];
String email2 = args[1];
Properties mailProperties = new Properties();
mailProperties.setProperty("protocol", "smtp");
mailProperties.setProperty("type", "transport");
mailProperties.setProperty("class", "com.sun.mail.smtp.SMTPTransport");
mailProperties.setProperty("mail.smtp.host",
System.getProperty("mail.smtp.host", // say on cmd line: -Dmail.smtp.host=xxx.yyy.zzz
"smtp.mycompany.com" // default if above not present
));
// setup the sending "session"
Session session = Session.getInstance(mailProperties, null);
String htmlString = "<html><body>This has <i>HTML</i> in it. google.</body></html>";
String txtString = "Hi text";
Address[] recipients = {
new InternetAddress(email1, "Smitty"),
new InternetAddress(email2)
};
// Assemble a MIME message
Message msg = new MimeMessage(session);
msg.addRecipients(Message.RecipientType.TO, recipients);
msg.setFrom(new InternetAddress("do-not-reply@mycompany.com", "My Company, Inc."));
msg.setSubject("Thanks for contacting us");
msg.setSentDate(new Date());
msg.setHeader("X-Mailer", "MyMailer $Name$");
MimeMultipart mp = new MimeMultipart("alternative");
MimeBodyPart txtPart = new MimeBodyPart();
txtPart.setText(txtString);
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlString, "text/html");
// it's probably important to have the txt part added first
// for the sake of less capable clients
mp.addBodyPart(txtPart);
mp.addBodyPart(htmlPart);
msg.setContent(mp);
Transport.send(msg);
}
}
Tuesday, January 16, 2007
Subscribe to:
Posts (Atom)